<?php
/**
 * Created by PhpStorm.
 * User: Star
 * Date: 2018-04-16
 * Time: 15:38
 */

namespace App\Http\Controllers;



use App\Member;
use Illuminate\Http\Request;

class MemberController extends Controller
{
    private $member;
    public function __construct(Member $member)
    {
        $this->member = $member;
    }

    public function member_list(Request $request)
    {
        $where = [];
        if ($request->has('name')) {
            $where['name'] = $request->name;
        }
        if ($request->has('start_time')) {
            $where['start_time'] = $request->start_time;
        }
        if ($request->has('end_time')) {
            $where['end_time'] = $request->end_time;
        }

        $lists = $this->member->getList($where);

        return view('member_list',['lists' => $lists,'request' => $request]);
    }

    public function member_add(Request $request)
    {
        if ($request->isMethod('POST')) {
            $data =[
                'name' => $request->name,
                'phone' => $request->phone,
                'email' => $request->email,
            ];
            $res = $this->member->add($data);
            if ($res) {
                return 1;
            }
            return 0;
        }
        return view('member_add');
    }

    public function member_edit(Request $request)
    {
        $id = $request->id;
        if ( $request->isMethod('POST')) {
            $data =[
                'name' => $request->name,
                'phone' => $request->phone,
                'email' => $request->email,
            ];
            $res = $this->member->find($id)->edit($data);
            if ($res) {
               return 1;
            }
            return 0;
        }else {
            $member = $this->member->find($id);
            if ($member) {
                $data =[
                    'id' => $member->id,
                    'name' => $member->name,
                    'phone' => $member->phone,
                    'email' => $member->email,
                ];
                return view('member_edit',['member' => $data]);
            }
            return 0;
        }
    }

    public function member_del(Request $request)
    {
        if ($request->isMethod('POST')) {
            $id = $request->id;
            if (!is_array($id)) {
                $id = [$id];
            }
            $res = $this->member->remove($id);
            if ($res) {
                return 1;
            }
            return 0;
        }
        return view('member_del');
    }

    public function member_password(Request $request)
    {
        return view('member_password');
    }


}